package krautz.fitness;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class FoodItemList extends Activity implements OnClickListener {
	
	private String restaurant;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getIntent().getExtras();
		restaurant = bundle.getString("restaurant");
		try{
			setContentView(R.layout.fooditem);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		/* Find Tablelayout defined in main.xml */
		
        TableLayout tl = (TableLayout)findViewById(R.id.food_items);
        float displayScale = getResources().getDisplayMetrics().density;
		FoodDatabase fd = new FoodDatabase(this);

		fd.openDataBase();
		Cursor c = fd.query("select * from FoodItems where restaurant='"+restaurant+"'", null);

		while (c.moveToNext()) {
			TableRow tr = new TableRow(this);
			tr.setOnClickListener(this);
			
			//first child is the food item foodname, calories, totalfat
			TextView t1 = new TextView(this);
			t1.setText(c.getString(c.getColumnIndex("foodname")));
			t1.setTextSize(	android.util.TypedValue.COMPLEX_UNIT_PX, 18);
			t1.setLayoutParams(new LayoutParams((int) (100*displayScale),LayoutParams.WRAP_CONTENT,1));
			tr.addView(t1);
			
			TextView t2 = new TextView(this);
			t2.setText(c.getString(c.getColumnIndex("calories")));
			t2.setTextSize(	android.util.TypedValue.COMPLEX_UNIT_PX, 18);
			t2.setLayoutParams(new LayoutParams((int) (30*displayScale),LayoutParams.WRAP_CONTENT,1));
			tr.addView(t2);
			
			TextView t3 = new TextView(this);
			t3.setText(c.getString(c.getColumnIndex("totalfat")));
			t3.setTextSize(	android.util.TypedValue.COMPLEX_UNIT_PX, 18);
			t3.setLayoutParams(new LayoutParams((int) (30*displayScale),LayoutParams.WRAP_CONTENT,1));
			tr.addView(t3);
			
			tl.addView(tr);
			
		}
		
	}

	public void onClick(View v) {
		TableRow tr = (TableRow)v;
		String text = ""+((TextView)tr.getChildAt(0)).getText();
		//Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
		//toast.show();
		
		Intent nutritionFactsWebView = new Intent(FoodItemList.this, NutritionFactsWebView.class);
		Bundle bundle = new Bundle();
		//bundle.putString("restaurant", restaurant);
		//bundle.putString("foodname", text);
		nutritionFactsWebView.putExtras(bundle);
		startActivity(nutritionFactsWebView);
	}
}
